<?php

namespace app\admin\controller\ad;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * @icon fa fa-circle-o
 */
class Picture extends Backend
{
    
    /**
     * Ad模型对象
     */
    protected $model = null;
    protected $ad_types;
    protected $cats;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Ad');
    
        $lists = Db::name('type')->field('type_id, type_name')->where(array('is_delete' => 0))->select();
        
        $this->ad_types[0] = '首页';
        $this->ad_types[-1] = '服务大厅';
        $this->ad_types[-2] = '任务大厅';
        foreach ($lists as $key=>$list) {
            $this->ad_types[$list['type_id']] = $list['type_name'];
        }
        $this->view->assign('ad_types', $this->ad_types);
        
        $this->cats = $this->model->cats;
        $this->view->assign('cats', $this->cats);
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        
            $where =  array();
            $where['is_delete'] = 0;
        
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
        
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            
            foreach ($list as &$v) {
                $v['type_id'] = $this->ad_types[$v['type_id']];
                $v['cat_id']= $this->cats[$v['cat_id']];
            }
            unset($v);
        
            $result = array("total" => $total, "rows" => $list);
        
            return json($result);
        }
        //列表工具栏
        $btns = array('refresh', 'add');
        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add(){
        if ($this->request->isPost()) {
            $inputData = input();
            $add_data = array(
                'type_id' => $inputData['type_id'],
                'cat_id' => $inputData['cat_id'],
                'ad_name' => $inputData['ad_name'],
                'width' => $inputData['width'],
                'height' => $inputData['height'],
                'summary' => $inputData['summary'],
                'picture' => $inputData['picture'],
                'link' => $inputData['link']?$inputData['link']:'javascript:void(0);',
                'start_time' => $inputData['start_time'],
                'end_time' => $inputData['end_time'],
                'sort' => $inputData['sort'],
                'is_delete' => 0,
                'create_time' => time()
            );
            $re = Db::name('ad')->insert($add_data);
            if($re) {
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL){
        $id = $ids;
        $row = Db::name('ad')->where(array('id' => $id))->find();
        
        if ($this->request->isPost()) {
            $inputData = input();
            $save_data = array(
                'type_id' => $inputData['type_id'],
                'cat_id' => $inputData['cat_id'],
                'ad_name' => $inputData['ad_name'],
                'width' => $inputData['width'],
                'height' => $inputData['height'],
                'summary' => $inputData['summary'],
                'picture' => $inputData['picture'],
                'link' => $inputData['link']?$inputData['link']:'javascript:void(0);',
                'start_time' => $inputData['start_time'],
                'end_time' => $inputData['end_time'],
                'sort' => $inputData['sort'],
                'is_delete' => 0,
                'create_time' => time()
            );
            $re = Db::name('ad')->where(array('id' => $id))->update($save_data);
            if($re) {
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }
        
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = "")
    {
        $id = $ids;
        $re = Db::name('ad')->where(array('id' => $id))->setField('is_delete', 1);
        if($re) {
            $this->success('success');
        } else {
            $this->error('fail');
        }
    }

}
